In [1]:
# -*- coding: utf-8 -*-
# !/usr/bin/python
# Cross Wavelet Analysis (CWA) based on Maraun and Kurths(2004).
# http://www.nonlin-processes-geophys.net/11/505/2004/npg-11-505-2004.pdf
# author: Mabel Calim Costa
# INPE
# 23/01/2013
# reviewed --- 02/03/2018
"""
Created on Mon Jun 17 2013

@author: Mabel Calim Costa
"""
import numpy as np
import pylab
from pylab import *
import matplotlib.pyplot as plt
import cmath
import pandas as pd

Now, let's create an artificial signal to test it!

  • create a time series X (sine)
  • create a time series Y (cosine) = out of phase sine

In [2]:
# using the example : example_python3.6.ipynb
import numpy as np
from pylab import *
import waipy
z = np.linspace(0,2048,2048)
x = np.sin(50*np.pi*z)

data_norm_x = waipy.normalize(x)
result_x = waipy.cwt(data_norm_x, 1, 1, 0.125, 2, 4/0.125, 0.72, 6,mother='Morlet',name='x')
waipy.wavelet_plot('x', z, x, 0.03125, result_x)


1024 1024
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/numpy/core/numeric.py:492: ComplexWarning: Casting complex values to real discards the imaginary part
  return array(a, dtype, copy=False, order=order)

In [3]:
#let's create another artificial signal to compare with that!
z2 = np.linspace(0,1024,1024)
y = np.concatenate((np.sin(30*np.pi*z2),np.sin(10*np.pi*z2)),axis=0)
data_norm_y = waipy.normalize(y)
result_y = waipy.cwt(data_norm_y, 1, 1, 0.125, 2, 4/0.125, 0.72, 6,mother='Morlet',name='x')
waipy.wavelet_plot('y', z, y, 0.03125, result_y)


1024 1024
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/numpy/core/numeric.py:492: ComplexWarning: Casting complex values to real discards the imaginary part
  return array(a, dtype, copy=False, order=order)

Let's check the cross power wavelet spectrum!

  • note that arrows indicate in phase when pointing to the right and out of phase when pointing left.

In [4]:
cross_power, coherence, phase_angle = waipy.cross_wavelet(result_x['wave'],result_y['wave'])
figname = 'cp_freqchange.png'
waipy.plot_cross('signals', cross_power, phase_angle, z, result_x, result_y,figname)
#Arrows indicate in phase when pointing to the right and out of phase when pointing left.


/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/numpy/ma/core.py:2778: ComplexWarning: Casting complex values to real discards the imaginary part
  order=order, subok=True, ndmin=ndmin)

Now let's view the wavelet coherence analysis

for X and Y signals


In [6]:
cross_power, coherence, phase_angle = waipy.cross_wavelet(result_x['wave'],result_y['wave'])

figname = 'cohere_freqchange.png'
waipy.plot_cohere('signals',coherence,z,result_x, result_y,figname)



In [ ]: